
// F. Chaxel 2010

// must be commented out for the final autostart background application
// #define BACKGROUND_APP

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <errno.h>
#include <unistd.h>
#include <syslog.h>
#include <string.h>

#include <iostream>
using namespace std;

#include "App.h"



/////////////////////////////////////////////////////////////////////////////
// Do not modify this main, just change the code inside App.Run(), App.c file
// all code here seems to be written by Devin Watson
// http://www.netzmafia.de/skripten/unix/linux-daemon-howto.html
/////////////////////////////////////////////////////////////////////////////
int main(void) 
{

	App *theApp;

	#ifdef BACKGROUND_APP

	/* Our process ID and Session ID */
	pid_t pid, sid;
        
	/* Fork off the parent process */
	pid = fork();
	if (pid < 0) 
		exit(EXIT_FAILURE);

	/* If we got a good PID, then
	we can exit the parent process. */
	if (pid > 0)
		exit(EXIT_SUCCESS);


	/* Change the file mode mask */
	umask(0);
                
	/* Open any logs here */        
                
	/* Create a new SID for the child process */
	sid = setsid();
	if (sid < 0)
		exit(EXIT_FAILURE);       
       
	/* Change the current working directory */
	if ((chdir("/")) < 0)		
		exit(EXIT_FAILURE);
    
	cout <<"Deamon mode"<<endl;

	/* Close out the standard file descriptors */
	close(STDIN_FILENO);
	close(STDOUT_FILENO);
	close(STDERR_FILENO);
	#else
	cout <<"Normal mode"<<endl;
	#endif

	theApp=new App();
	theApp->Run();

	exit(EXIT_SUCCESS);
}
